/**
 * @project_name staff_vehicle
 * @package_name com.ssh.dao
 * @desc TODO
 * @param
 * @author jack
 * @date 2013-4-22
 * @time 下午8:41:06
 */
package com.ssh.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.ssh.entity.MvehicleTable;
import com.ssh.model.pdata.MvehicleVisitAllData;

/**
 * @author jack
 * @desc 军车访问接口实现
 */
public class MvehicleTableDAOImpl implements MvehicleTableDAOI {
	private SessionFactory sessionFactory;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.MvehicleTableDAOI#addMVV(com.ssh.entity.MvehicleTable)
	 */
	@Override
	public MvehicleTable addMVV(MvehicleTable mVVisitor) {
		// TODO Auto-generated method stub
		try {
			sessionFactory.getCurrentSession().save(mVVisitor);
		} catch (Exception e) {
			return null;
		}
		return mVVisitor;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.MvehicleTableDAOI#addMVV(com.ssh.entity.MvehicleTable[])
	 */
	@Override
	public int addMVV(MvehicleTable... mVVisitor) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.MvehicleTableDAOI#deleteMVV(java.lang.String[])
	 */
	@Override
	public int deleteMVV(String... mVVisitor) {
		// TODO Auto-generated method stub
		return 0;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.MvehicleTableDAOI#deleteMVVById(java.lang.String)
	 */
	@Override
	public int deleteMVVById(String mVVisitorId) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.MvehicleTableDAOI#queryMVV(java.lang.String[])
	 */
	@Override
	public List<MvehicleTable> queryMVV(String... mVVisitors) {
		// TODO Auto-generated method stub
		Query query = sessionFactory
				.getCurrentSession()
				.createQuery(
						"from MvehicleTable as mvehicleTable where mvehicleTable.mvehicleId = ?");
		query.setString(0, mVVisitors[0]);
		@SuppressWarnings("unchecked")
		List<MvehicleTable> mVVisitorsList = query.list();
		return mVVisitorsList;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.MvehicleTableDAOI#queryMVVById(java.lang.String)
	 */
	@Override
	public MvehicleTable queryMVVById(String mVVisitorId) {
		// TODO Auto-generated method stub

		return null;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.MvehicleTableDAOI#queryMVVByVIdIn(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public MvehicleTable queryMVVByVIdIn(String mVVisitorId, String state) {
		// TODO Auto-generated method stub
		String hql = "from MvehicleTable m where m.visitorId=:mVVisitorId and m.state=:state";
		return (MvehicleTable) sessionFactory.getCurrentSession()
				.createQuery(hql).setParameter("mVVisitorId", mVVisitorId)
				.setParameter("state", state).uniqueResult();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.MvehicleTableDAOI#queryMVVByVVId(java.lang.String,
	 * java.lang.String)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<MvehicleTable> queryMVVByVVId(String mVVisitorId,
			String mvehicleId) {
		// TODO Auto-generated method stub
		String hql = "from MvehicleTable m where m.visitorId=:mVVisitorId and m.mvehicleId=:mvehicleId";
		return sessionFactory.getCurrentSession().createQuery(hql)
				.setParameter("mVVisitorId", mVVisitorId)
				.setParameter("mvehicleId", mvehicleId).list();
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.ssh.dao.MvehicleTableDAOI#queryMVVById(java.lang.String)
	 */
	@Override
	public MvehicleTable queryMVVByVVId(String mVVisitorId, String vehicleId,
			String state) {
		// TODO Auto-generated method stub
		String hql = "from  MvehicleTable as m "
				+ "where m.visitorId =:mVVisitorId and m.mvehicleId=:vehicleId and m.state=:state";
		Query query =sessionFactory.getCurrentSession()
				.createQuery(hql).setParameter("mVVisitorId", mVVisitorId)
				.setParameter("vehicleId", vehicleId)
				.setParameter("state", state);
		return (MvehicleTable)query.uniqueResult();
	}

	@Override
	public MvehicleTable queryMVVByVVIdState(String vehicleId,
			String state) {
		// TODO Auto-generated method stub
		String hql = "from  MvehicleTable as m "
				+ "where m.mvehicleId=:vehicleId and m.state=:state";
		Query query =sessionFactory.getCurrentSession()
				.createQuery(hql).setParameter("vehicleId", vehicleId)
				.setParameter("state", state);
		return (MvehicleTable)query.uniqueResult();
	}


	/**
	 * @param sessionFactory
	 *            the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.MvehicleTableDAOI#updateMVV(com.ssh.entity.MvehicleTable)
	 */
	@Override
	public MvehicleTable updateMVV(MvehicleTable mVVisitor) {
		// TODO Auto-generated method stub
		sessionFactory.getCurrentSession().update(mVVisitor);
		return mVVisitor;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see
	 * com.ssh.dao.MvehicleTableDAOI#updateMVV(com.ssh.entity.MvehicleTable[])
	 */
	@Override
	public int updateMVV(MvehicleTable... mVVisitors) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public MvehicleVisitAllData queryAllInfo(String mVVisitorId, String vehicleId, String state) {
		// TODO Auto-generated method stub
		String hql = "select new com.ssh.model.pdata.MvehicleVisitAllData(m,vl,v) from  MvehicleTable as m, "
				+ " Vehicle vl,Visitor v where m.visitorId =:mVVisitorId and m.mvehicleId=:vehicleId and m.state=:state" +
				" and m.visitorId=v.visitorId and m.mvehicleId= vl.vehicleId ";
		Query query =sessionFactory.getCurrentSession()
				.createQuery(hql).setParameter("mVVisitorId", mVVisitorId)
				.setParameter("vehicleId", vehicleId)
				.setParameter("state", state);
		return (MvehicleVisitAllData)query.uniqueResult();
	}

	@Override
	public MvehicleVisitAllData queryAllInfo(String vehicleId, String state) {
		// TODO Auto-generated method stub
		String hql = "select new com.ssh.model.pdata.MvehicleVisitAllData(m,vl,v) from  MvehicleTable as m, "
				+ " Vehicle vl,Visitor v where  m.mvehicleId=:vehicleId and m.state=:state" +
				" and m.visitorId=v.visitorId and m.mvehicleId= vl.vehicleId ";
		Query query =sessionFactory.getCurrentSession()
				.createQuery(hql).setParameter("vehicleId", vehicleId)
				.setParameter("state", state);
		return (MvehicleVisitAllData)query.uniqueResult();
	}
}
